package com.ruifu.bom.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.ruifu.bom.service.MaterielDocumentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物料文档控制层
 * @author lcq
 * @create 2020/1/2
 */
@RestController
@RequestMapping("/materielDocument")
@Slf4j
public class MaterielDocumentController {
    @Autowired
    MaterielDocumentService materielDocumentService;

//    /**
//     * 新建关联图档
//     *
//     * @param materielDocumentDTO 物料文档传输对象
//     * @return
//     */
//    //@PostMapping("/save")
//    public R relevanceDocument(@RequestBody MaterielDocumentDTO materielDocumentDTO) {
//        try {
//            materielDocumentService.  save(materielDocumentDTO);
//            return R.ok("新建成功！");
//        } catch (Exception e) {
//            log.error("新建关联图档异常：", e);
//            return R.failed(e.getMessage());
//        }
//    }

    /**
     * 新建关联图档
     *
     * @param mdVersionsId
     * @param dcVerIds
     * @return
     */
    @PostMapping("/{mdVersionsId}")
    public R addRelevanceDocument(@PathVariable("mdVersionsId") Long mdVersionsId, @RequestBody List<Long> dcVerIds) {
        try {
            materielDocumentService.addBatch(mdVersionsId, dcVerIds);
            return R.ok("新建成功！");
        } catch (Exception e) {
            log.error("新建关联图档异常：", e);
            return R.failed(e.getMessage());
        }
    }

    /**
     * 根据物料版本id查询图档
     *
     * @param mdVersionsId 物料版本id
     * @return
     */
    @GetMapping("/{mdVersionsId}")
    public R findByMdVersionsId(@PathVariable("mdVersionsId") Long mdVersionsId) {
        return R.ok(materielDocumentService.findByMdVersionsId(mdVersionsId)) ;

    }

    /**
     * 删除关联图档
     * @param mdIds
     * @return
     */
    @PostMapping("/deleteBatch")
    public R deleteById(@RequestBody List<Long> mdIds) {
        try {
            return materielDocumentService.deleteBatch(mdIds);
        } catch (Exception e) {
            log.error("删除关联图档异常：", e);
            return R.failed(e.getMessage());
        }
    }
}
